package com.hjqjl.module_home.ui.list.recyclerview;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hjqjl.module_home.databinding.HomeItemFruitlistBinding;
import com.hjqjl.module_home.model.Fruit;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
    private List<Fruit> mFruitList;

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView homeIvFruitListItemIc;
        TextView homeTvFruitListItemName;

        public ViewHolder(HomeItemFruitlistBinding binding) {
            super(binding.getRoot());
            homeIvFruitListItemIc = binding.homeIvFruitListItemIc;
            homeTvFruitListItemName = binding.homeTvFruitListItemName;
//            fruitImage = (ImageView) view.findViewById(R.id.home_iv_fruitList_item_ic);
//            fruitName = (TextView) view.findViewById(R.id.home_tv_fruitList_item_name);
        }
    }

    public FruitAdapter(List<Fruit> fruitList) {
        mFruitList = fruitList;
    }

    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
        HomeItemFruitlistBinding binding = HomeItemFruitlistBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
//        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_item_fruitlist, parent, false);
//        ViewHolder holder = new ViewHolder(view);
        ViewHolder holder = new ViewHolder(binding);
        holder.itemView.setOnClickListener(v -> {
            int position = holder.getAdapterPosition();
            Fruit fruit = mFruitList.get(position);
            ToastUtils.showShort("position:" + position + "==我是itemView==" + fruit.getName());
        });
        holder.homeIvFruitListItemIc.setOnClickListener(v -> {
            int position = holder.getAdapterPosition();
            LogUtils.v("" + position);
            Fruit fruit = mFruitList.get(position);
            ToastUtils.showShort("position:" + position + "==我是图片控件homeIvFruitListItemIc=="
                    + fruit.getName());
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Fruit fruit = mFruitList.get(position);
        holder.homeIvFruitListItemIc.setImageResource(fruit.getImageId());
        holder.homeTvFruitListItemName.setText(fruit.getName());
    }

    @Override
    public int getItemCount() {
        return mFruitList.size();
    }
}